//
//  PywModifyLoginPwdViewController.m
//  PywSdk
//
//  Created by smallwave on 16/8/11.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywModifyLoginPwdViewController.h"
#import "PywTextFieldView.h"
#import "UIButton+Pyw.h"
#import "PywCommonManager.h"
#import "PywMasonry.h"
#import "PywSetNewPwdApi.h"
#import "PywForgetCheckPhoneViewController.h"
#import "PywPlatformMain.h"
#import "PywUser.h"
#import "PywPassportUtils.h"
#import "PywLogoutManger.h"
#import "PywSdk+login.h"
#import "PywMBProgressHUD+Pyw.h"
#import "MutableDictionary.h"
#import "PywCPNotifications.h"
#import "PywUploadAccountLoginManager.h"

@interface PywModifyLoginPwdViewController ()

@property (nonatomic, strong) PywTextFieldView  *oldLoginPwdText;   // 旧密码输入框
@property (nonatomic, strong) PywTextFieldView  *newsLoginPwdText;  // 新密码输入框
@property (nonatomic, strong) UIButton          *modifyBtn;         // 修改按钮

@property (nonatomic, strong) PywSetNewPwdApi   *setNewPwdApi;      // 修改密码Api

@end

@implementation PywModifyLoginPwdViewController
KeyBoardHelper(90)

#pragma mark - override
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self  setUp];
}
#pragma mark - prepareUI
- (void)setUp{
    self.title = @"修改登录密码";
    self.navigationItem.rightBarButtonItem = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    [self.view addSubview:self.oldLoginPwdText];
    [self.view addSubview:self.newsLoginPwdText];
    [self.view addSubview:self.modifyBtn];
    
    __weak typeof(self) wSelf = self;
    [self.oldLoginPwdText mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
        make.height.equalTo(@40);
    }];
    [self.newsLoginPwdText mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.oldLoginPwdText.mas_bottom);
        make.width.height.equalTo(wSelf.oldLoginPwdText);
    }];
    [self.modifyBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.newsLoginPwdText.mas_bottom).offset(40);
        make.width.height.equalTo(wSelf.newsLoginPwdText);
    }];
}
#pragma mark - Action
/**
 *  修改密码
 */
- (void)modifyLoginPwdAction{
    [self.view endEditing:YES];
    
    if (self.oldLoginPwdText.text.length == 0
        || self.newsLoginPwdText.text.length == 0) {
        [PywMBProgressHUD hideHUD];
        [PywMBProgressHUD showError:@"新旧密码均不能为空！"];
        
    } else if (self.oldLoginPwdText.text.length < 6
        || self.newsLoginPwdText.text.length < 6) {
        [PywMBProgressHUD hideHUD];
        [PywMBProgressHUD showError:@"新旧密码均不能低于6位！"];
        
    } else {

        MutableDictionary *params = [self requestSetNewPwdParams];
        [self.setNewPwdApi requestSetNewPwdWithParams:params success:^(NSString *status, id anyObjct) {
            // 清空本地当前账号的密码
            PywUser *user = [[PywUser alloc] init];
            user.passport = [PywPlatformMain sharedPlatformMain].userName;
            user.password = @"";
            [PywPassportUtils savePassport:user];
            
            // 注销账号，保存当前登录账号结束时间
            [[PywUploadAccountLoginManager manager] saveAccountLoginEndTime];
            
            // 注销
            [[PywLogoutManger manger] clearLogin];
            
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                // 发送注销完成的通知
                NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                [dict setValue:@"注销成功" forKey:@"info"];
                [dict setValue:[NSString stringWithFormat:@"%d",PYW_LOGOUT_STATE_NORMAL] forKey:@"result"];
                [[NSNotificationCenter defaultCenter] postNotificationName:kPywCPLogoutNotification object:nil userInfo:dict];
            }];
            
        } fail:^(NSError *error) {
            
        }];
    }
    
}
/**
 *  修改密码  传入的参数
 */
- (MutableDictionary *)requestSetNewPwdParams{
    NSString *old_pwd = [self.oldLoginPwdText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *news_pwd = [self.newsLoginPwdText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *username = [[PywPlatformMain sharedPlatformMain].userName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    MutableDictionary *params = [[MutableDictionary alloc] init];
    [params setObject:old_pwd forKey:@"old_pwd"];
    [params setObject:news_pwd forKey:@"new_pwd"];
    [params setObject:username forKey:@"username"];
    return params;
}

#pragma mark - Lazy
- (PywTextFieldView *)oldLoginPwdText{
    if (!_oldLoginPwdText) {
        _oldLoginPwdText = [PywTextFieldView passwordTextFieldWithLeftImg:nil
                                                          TextPlaceholder:@"请输入旧密码"
                                                          ButtonNormalImg:nil
                                                        ButtonSelectedImg:nil];
        _oldLoginPwdText.secureTextEntry = YES;
        [_oldLoginPwdText setAccessibilityId:@"modify_oldpsw"];
    }
    return _oldLoginPwdText;
}
- (PywTextFieldView *)newsLoginPwdText{
    if (!_newsLoginPwdText) {
        _newsLoginPwdText = [PywTextFieldView passwordTextFieldWithLeftImg:nil
                                                           TextPlaceholder:@"请输入新密码"
                                                           ButtonNormalImg:@"icon_visible_normal"
                                                         ButtonSelectedImg:@"icon_visible_disabled"];
        [_newsLoginPwdText setAccessibilityId:@"modify_newpsw"];
    }
    return _newsLoginPwdText;
}
- (UIButton *)modifyBtn{
    if (!_modifyBtn) {
        _modifyBtn = [UIButton buttonWithBackgroundColor:RGB(252, 177, 14, 1)
                                            cornerRadius:20
                                              titleColor:[UIColor whiteColor]
                                      titleLabelFontSize:18
                                                   title:@"立即修改"];
        
        [_modifyBtn addTarget:self action:@selector(modifyLoginPwdAction) forControlEvents:UIControlEventTouchUpInside];
        [_modifyBtn setAccessibilityIdentifier:@"modify_btn"];
    }
    return _modifyBtn;
}

- (PywSetNewPwdApi *)setNewPwdApi{
    if (!_setNewPwdApi) {
        _setNewPwdApi = [PywSetNewPwdApi manager];
    }
    return _setNewPwdApi;
}

@end
